﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.OleDb;
using System.Data;

namespace ESOL.Common
{
    public class DwgFile
    {

        public static DataTable ReadDwg(string path)
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            for (int i = 0; i < 8; i++)
            {
                dt.Columns.Add(new System.Data.DataColumn("", typeof(object)));
            }
            string content = "";
            string filename = "";
            FileInfo finfo = new FileInfo(path);
            filename = finfo.Name;
            Autodesk.AutoCAD.Interop.AcadApplication ap = new Autodesk.AutoCAD.Interop.AcadApplication();
            lock (ap)
            {
                try
                {
                    Autodesk.AutoCAD.Interop.AcadDocument ad = ap.Documents.Open(path, true, null);
                    System.Threading.Thread.Sleep(2000);
                    int n = 0;
                    for (int i = 0; i < ad.ModelSpace.Count; i++)
                    {
                        string text = "";
                        if (ad.ModelSpace.Item(i).ObjectName.ToLower().Equals("acdbmtext"))
                        {
                            Autodesk.AutoCAD.Interop.Common.AcadMText im = (Autodesk.AutoCAD.Interop.Common.AcadMText)ad.ModelSpace.Item(i);
                            if (im.TextString.Contains("\\P"))
                            {
                                content += im.TextString + "\n";
                                text = im.TextString;
                            }
                        }
                        if (string.IsNullOrEmpty(text) == false)
                        {
                            string[] arg = text.Replace("\\P", "@").Split(new char[] { '@' });
                            for (int j = 0; j < arg.Length; j++)
                            {
                                while (dt.Rows.Count <= j)
                                {
                                    dt.Rows.Add();
                                }
                                dt.Rows[j][n] = arg[j];
                            }
                            n++;
                        }
                    }
                    ad.Close(false, filename);
                }
                finally
                {

                    ap.Quit();
                }
            }
            for (int l = 0; l < dt.Rows.Count; l++)
            {
                dt.Rows[l][7] = dt.Rows[l][7].ToString().Replace("%%C", "Φ");
            }
            return dt;
        }

        public static DataTable Readxls(string path)
        {
            string strCon = "";
            if (path.Substring(path.Length - 1).Equals("x"))
            {
                strCon = " Provider = Microsoft.ACE.OLEDB.12.0 ; Data Source =" + path + ";Extended Properties=\"Excel 12.0;HDR=YES\"";
            }
            else
            {
                strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + path + ";Extended Properties=Excel 8.0";
            }

            OleDbConnection conn = new OleDbConnection(strCon);
            string tableName = "sheet1$";
            conn.Open();
            DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            if (dt != null && dt.Rows.Count > 0)
            {
                tableName = dt.Rows[0]["TABLE_NAME"].ToString();
            }
            string sql = "select * from [" + tableName + "]";
            OleDbDataAdapter cmd = new OleDbDataAdapter(sql, strCon);
            DataSet ds = new DataSet();
            cmd.Fill(ds, "FormObjects");
            conn.Close();
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }
    }
}
